<?php
namespace app\admin\controller;

use app\admin\model\Admin;
use app\common\model\RecyclingList;
use think\Db;
use think\Validate;

class Recycling extends Common
{
    public function type_list(){
        $list = Db::name('recycling_type')->field('id,title,describe,is_open')->select();
        return view('',['list'=>$list]);
    }
    public function edit(){
        if(Request()->isPost()) {
            $id = input('param.id', 0);
            if (!$id) {
                return package(20001, 'id错误');
            }
            $param = input('param.');
            $rule = [
                'title|类型' => 'require|max:40',
                'describe|文章标题' => 'require|max:60',
            ];
            $validate = Validate::make($rule);
            if(isset($param['is_open']) && $param['is_open'] =='on'){
                $param['is_open']  = 1;
            }else{
                $param['is_open']  = 0;
            }
            if (!$validate->check($param)) {
                return package(10002,$validate->getError());
            }
            $res = Db::name('recycling_type')->where('id',$id)->update([
                'title'=>$param['title'],
                'describe'=>$param['describe'],
                'is_open'=>$param['is_open']
            ]);
            return checkRes($res,'修改');
        }else{
            $id = input('param.id');
            $info = Db::name('recycling_type')->where('id',$id)->find();
            return view('',['info'=>$info]);
        }
    }
    public function recycling_list(){
        $type_id = input('param.type_id',1);
        $list = Db::name('recycling_list')
            ->where('type_id',$type_id)
            ->field('id,name,describe,unit,reference_price,create_time,type_id')
            ->select();
        $listt = Db::name('recycling_type')->field('title,id')->select();
        return view('',['list'=>$list,'type_list'=>$listt,'type_id'=>$type_id]);
    }

    public function add_recycling(){
        $type_id = input('param.type_id',1);
        if(!$type_id){
            $this->error('id错误');
        }
        if(Request()->isPost()) {
            $data = input('param.');
            $model = new RecyclingList();
            $res = Validate::make()
                ->rule([
                    'name|分类名'=>"require|max:15",
                    'describe|描述' =>'max:40',
                    'type_id|类型'=>'require|max:11',
                    'unit|单位'=>'require|max:10',
                    'reference_price|参考'=>'require|float|max:5',
                ]);
            if (!$res->check($data)) {
                return  clientReturnJson('10006',$res->getError());
            }
            $array = [
                'name'=>$data['name'],
                'describe'=>$data['describe'],
                'type_id'=>$data['type_id'],
                'create_time'=>date("Y-m-d H:i:s"),
                'reference_price'=>$data['reference_price'],
                'unit'=>$data['unit']
            ];
            $result = $model->add($array);
            return checkRes($result,'添加');

        }
        $list = Db::name('recycling_type')->field('title,id')->select();
        return view('',['type_id'=>$type_id,'type_list'=>$list]);
    }
    public function edit_recycling(){
        $type_id = input('param.type_id');
        if(!$type_id){
            $this->error('id错误');
        }
        $id = input('param.id');
        if(Request()->isPost()) {
            $data = input('param.');
            $model = new RecyclingList();
            $res = Validate::make()
                ->rule([
                    'name|分类名'=>"require|max:15",
                    'describe|描述' =>'max:40',
                    'type_id|类型'=>'require|max:11',
                    'unit|单位'=>'require|max:10',
                    'reference_price|参考'=>'require|float|max:5',

                ]);
            if (!$res->check($data)) {
                return  clientReturnJson('10006',$res->getError());
            }
            $array = [
                'name'=>$data['name'],
                'describe'=>$data['describe'],
                'type_id'=>$data['type_id'],
                'create_time'=>date("Y-m-d H:i:s"),
                'reference_price'=>$data['reference_price'],
                'unit'=>$data['unit']
            ];
            $result = $model->edit($array,$data['id']??0);
            return checkRes($result,'添加');
        }
        $list = Db::name('recycling_type')->field('title,id')->select();
        $info = Db::name('recycling_list')->where('id',$id)->find();
        return view('add_recycling',['type_id'=>$type_id,'type_list'=>$list,'info'=>$info]);
    }
    public function setRecycling(){

        if(Request()->isPost()) {

            $data = input('post.');
            $arr = [];
            $time = date("Y-m-d H:i:s");
            foreach ($data as $k=>$v){
                $d = explode('_',$k);
                $arr[] = [
                    'list_id'=>$d[0],
                    'region_id'=>$d[1],
                    'price'=>$v,
                    'create_time'=>$time
                ];
            }
            $result = Db::name('recycling_reg_price')->insertAll($arr,true);
            return checkRes($result,'修改');
        }
        $id = input('param.type_id');
        $where =[];
        if(ADMINTYPE ==2){
            $where[] = ["id","=",$this->adminInfo["region_id"]];
        }
        $data = Db::name('region')->where('open',1)->where($where)->field('id as region_id,name')->select();
        $ins = '';
        foreach ($data as $v){
            $ins.=$v['region_id'].',';
        }
        $in []  = ['list_id','=',$id];
        if(ADMINTYPE ==1){
            $in []  = ['region_id','in',$ins];
        }else{
            $in []  = ['region_id','=',$this->adminInfo["region_id"]];
        }
        $list = Db::name('recycling_reg_price')->where($in)->field('list_id,region_id,price')->select();
        foreach ($list as $k=>$v){
            foreach ($data as $vv){
                if($vv['region_id'] ==$v['region_id']){
                    $data[$k]['price'] = $v['price'];
                }
            }
        }
       return view('',['list'=>$data,'reference_price'=>Db::name('recycling_list')->where('id',$id)->value('reference_price')]);
    }
}